import { createAction, actionTypeCreatorWithNameSpace } from './util';
import agent from 'agent';

const actionTypeCreator = actionTypeCreatorWithNameSpace('runnableGroup');

export const listByCurrentUser = createAction(actionTypeCreator('listByCurrentUser'), () => {
  return agent.get('/api/users/current/runnable-groups')
    .then(response => response.body);
}, {
  fetch: {
    processing: true
  }
});

export const assign = createAction(actionTypeCreator('assign'), (groupId, teamId) => {
  return agent.post(`/api/runnable-groups/${groupId}/assign`)
    .type('form')
    .send({ teamId })
    .then(() => {
      return { groupId, teamId };
    });
});

export const updateMachines = createAction(actionTypeCreator('updateMachines'), (groupId, machineIds) => {
  return agent.post(`/api/runnable-groups/${groupId}/machines`)
    .type('form')
    .send({ machineIds })
    .then(() => {
      return { groupId, machineIds };
    });
});

export const get = createAction(actionTypeCreator('get'), (id) => {
  return agent.get(`/api/runnable-groups/${id}`)
    .then(response => response.body);
});

export const listGroupMachines = createAction(actionTypeCreator('listGroupMachines'), (id) => {
  return agent.get(`/api/runnable-groups/${id}/machines`)
    .then(response => response.body);
}, {
  fetch: {
    processing: true,
    cache: true
  }
});

export const listUnitsByRuntimeId = createAction(actionTypeCreator('listUnitsByRuntimeId'), (runtimeId) => {
  return agent.get(`/api/runnable-runtimes/${runtimeId}/runnable-units`)
    .then(response => response.body);
}, (runtimeId) => {
  return {
    runtimeId,
    fetch: {
      processing: true,
      processingKey: runtimeId,
      cache: {
        key: runtimeId
      }
    }
  };
});

export const createRuntime = createAction(actionTypeCreator('createRuntime'), (params) => {
  return agent.post('/api/runnable-runtimes')
    .type('form')
    .send(params)
    .then(response => {
      const id = parseInt(response.body, 10);
      return {
        id,
        groupId: params.groupId,
        name: params.name
      };
    });
});

export const deleteRuntime = createAction(actionTypeCreator('deleteRuntime'), (runtimeId) => {
  return agent.del(`/api/runnable-runtimes/${runtimeId}`)
    .then(() => runtimeId);
});

export const renameRuntime = createAction(actionTypeCreator('renameRuntime'), (runtimeId, name) => {
  return agent.put(`/api/runnable-runtimes/${runtimeId}/rename`)
    .type('form')
    .send({ name })
    .then(() => {
      return { runtimeId, name };
    });
});

export const search = createAction(actionTypeCreator('search'), (query) => {
  return agent.get('/api/runnable-groups')
    .query(query)
    .then(response => response.body);
}, {
  fetch: {
    processing: true
  }
});

export const create = createAction(actionTypeCreator('create'), (runnableGroup) => {
  return agent.post('/api/runnable-groups')
    .send(runnableGroup)
    .then(response => response.body);
});

export const update = createAction(actionTypeCreator('update'), (groupId, runnableGroup) => {
  return agent.put(`/api/runnable-groups/${groupId}`)
    .send(runnableGroup)
    .then(response => response.body);
});

